package cn.xopencode.appletweb.controller.user;


import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.security.annotations.RequiresNone;
import cn.xopencode.userservice.rpc.user.UserRpc;
import cn.xopencode.userservice.rpc.user.dto.*;
import cn.xopencode.web.core.util.CommonWebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private HttpServletRequest request;

    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;

    @ApiOperation(value = "查询单个用户信息")
    @GetMapping("/get/{userId}")
    public CommonResult<UserDTO> getUser(@PathVariable("userId") String userId) {
        return userRpc.getUser(userId);
    }

    @ApiOperation(value = "获取指定用户同一部门下的所有用户")
    @GetMapping("/get/dept/{userId}")
    public CommonResult<List<UserDTO>> getUserList(@PathVariable("userId") String userId) {
        return userRpc.getUserList(userId);
    }

    @ApiOperation(value = "根据参数获取用户信息")
    @PostMapping("/get/list")
    public CommonResult<List<UserDTO>> getUserList(@RequestBody UserParamDTO userParamDTO) {
        return userRpc.getUserList(userParamDTO);
    }

    @ApiOperation(value = "用户列表分页查询")
    @PostMapping("/list")
    public CommonResult<PageResult<UserDTO>> findUserList(@RequestBody UserPageDTO userPageDTO) {
        return userRpc.findUserList(userPageDTO);
    }

    @RequiresNone
    @ApiOperation(value = "新建用户")
    @PostMapping("/create")
    public CommonResult<String> createUser(@RequestBody CreateUserDTO createUserDTO, HttpServletRequest request) {
        createUserDTO.setUserId(CommonWebUtil.getUserId(request));
        return userRpc.createUser(createUserDTO);
    }

    @ApiOperation(value = "批量更新用户信息")
    @PostMapping("/update")
    public CommonResult<Integer> updateUser(@RequestBody List<UpdateUserDTO> updateUserDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updateUserDTOList.forEach(updateUserDTO -> {
            updateUserDTO.setUserId(userId);
        });
        return userRpc.updateUser(updateUserDTOList);
    }

    @ApiOperation(value = "批量更改用户属性")
    @PostMapping("/update/pro")
    public CommonResult<Integer> updateProperty(@RequestBody List<UpdatePropertyDTO> updatePropertyDTOList) {
        String userId = CommonWebUtil.getUserId(request);
        updatePropertyDTOList.forEach(updatePropertyDTO -> {
            updatePropertyDTO.setUserId(userId);
        });
        return userRpc.updateProperty(updatePropertyDTOList);
    }
}
